$(function () {
    var shopdata;
    $.ajax({
        'type': 'POST',
        'url': '/withdraw/getshop',
        'async': false,
        'dataType': 'json',
        'success': function (data) {
            shopdata = data.data;
        }
    });

    $('.ispwd').click(function () {
        var isbtn = $(this).attr('attrs');
        var pwd = $('.hidpwd').val();
        //验证是否输入密码
        if(pwd){
            if(isbtn == 'btnwx') {
                layer.msg('提现到微信暂未开放');return;
                var openid = shopdata.openid;
                if(!openid){
                    var codeurl = $('.ewmurl').val();
                    layer.open({
                        id: 5,
                        type: 1,
                        title: '请扫描小程序二维码绑定商户信息',
                        skin: 'layui-layer-rim',
                        area: ['auto', '530px'],
                        content: '<img src="'+codeurl+'">',
                        btn: ['绑定完成后请刷新页面', '取消'],
                        btn1: function (index, layero) {
                            window.location.reload()
                        }
                    })
                    return;
                }
                var shopmoneyss = getshopmoney();
                layer.open({
                    id: 2,
                    type: 1,
                    title: '请输入提现金额',
                    skin: 'layui-layer-rim',
                    area: ['350px', 'auto'],
                    content: '<div style="margin-top: 20px;margin-left: 5px">' +
                    '<p class="boxtop"><span class="txtitle">可提现余额</span><span class="rmb">¥' + shopmoneyss + '</span></p>' +
                    '<p class="boxtop"><span class="txtitle">提现金额</span>￥<input type="test" class="txmoney" ></p>' +
                    '<p class="txtexts"></p>' +
                    '</div>',
                    btn: ['提交提现申请', '取消'],
                    btn1: function (index, layero) {
                        var txmoneys = $('.txmoney').val();

                        if (parseFloat(txmoneys) > parseFloat(shopmoneyss)) {
                            layer.msg('提现金额不能超过可提现余额');return;
                        } else {
                            //TODO 微信提现
                            $.ajax({
                                'type': 'POST',
                                'url': '/withdraw/add-wihdraw',
                                'data': {withdraw_money: txmoneys,type:0},
                                'dataType': 'json',
                                'success': function (data) {
                                    if(data.code == 200){
                                        var shopmoney;
                                        layer.msg('提现申请成功,待审核')
                                        layer.close(index);
                                        $('.yueval').html(data.data.money);
                                        $('.txcount').html(data.data.count);
                                    } else {
                                        layer.msg(data.msg)
                                    }
                                }
                            });
                        }
                    }
                })
            } else if(isbtn == 'btnyhk'){
                var shopmoneyss = getshopmoney();
                layer.open({
                    id: 6,
                    type: 1,
                    title: '请输入提现金额',
                    skin: 'layui-layer-rim',
                    area: ['350px', 'auto'],
                    content:  '<div style="margin-top: 20px;margin-left: 5px">' +
                    '<p class="boxtop"><span class="txtitle">可提现余额</span><span class="rmb"></span>¥'+ shopmoneyss +'</p>' +
                    '<p class="boxtop"><span class="txtitle">提现金额</span>￥<input type="test" class="txmoneys" onkeydown="xzasdasd(this)"   ></p>' +
                    '<p class="txtexts"></p>' +
                    '<p class="boxtop"><span class="txtitle">服务费</span><span class="rmb fwf">￥</span></p>' +
                    '<p class="txtexts">微信平台转账手续费率 0.1%</p>' +
                    '</div>',
                    btn: ['下一步', '取消'],
                    btn1: function (index,layero) {
                        var txmoneys = $('.txmoneys').val();
                        if(txmoneys ==''){
                            layer.msg('请输入提现金额');return;
                        }
                        if (parseFloat(txmoneys) > parseFloat(shopmoneyss)) {
                            layer.msg('提现金额不能超过可提现余额');return;
                        }
                        var wit ;
                        $.ajax({
                            'type':'POST',
                            'url':'/withdraw/get-withdraw-info',
                            'async': false,
                            'dataType':'json',
                            'success':function(data)
                            {
                                wit = data.data;
                            }
                        });

                        layer.close(index);
                        var options = '';
                        $.each(shopdata.yhk_list,function (it,i) {
                            options += '<option value="'+it+'"   '+((wit.bank_code == it)?" selected ":" ")+' >'+i+'</option>'
                        })
                        layer.open({
                            id: 7,
                            type: 1,
                            title: '提现银行卡信息',
                            skin: 'layui-layer-rim',
                            area: ['450px', 'auto'],
                            content:  '<div style="margin-top: 20px;margin-left: 5px">' +
                            '<p class="boxtop"><span class="txtitle">开户人姓名</span><input type="test" value="'+wit.bank_name+'" class="khr" ></p>' +
                            '<p class="boxtop"><span class="txtitle">开户行</span>' +
                            '<select class="khh">' +
                            '<option value="0">请选择开户行</option>' +
                            options +
                            '</select>' +
                            '</p>' +
                            '<p class="boxtop"><span class="txtitle">转账银行卡</span><input type="test" class="zzyhk" value='+wit.bank_no+' ></p>' +
                            '<p class="txtexts"></p>' +
                            '<input type="hidden" class="txmoneys" value="'+txmoneys+'">' +
                            '</div>',
                            btn: ['下一步', '取消'],
                            btn1: function (index, layero) {
                                var txmoneys = $('.txmoneys').val();
                                var zzyhk = $('.zzyhk').val();
                                var khh = $('.khh').val();
                                var khr = $('.khr').val();

                                if(zzyhk ==''){
                                    layer.msg('请输入银行卡号');return;
                                } else if(khh == '') {
                                    layer.msg('请选择开户行');return;
                                } else if(khr == ''){
                                    layer.msg('请输入开户人姓名');return;
                                }

                                layer.close(index)
                                layer.open({
                                    id: 8,
                                    type: 1,
                                    title: '管理员手机短信验证',
                                    skin: 'layui-layer-rim',
                                    area: ['280px', 'auto'],
                                    content: '<div style="margin-top: 20px;margin-left: 5px">' +
                                    '<p class="boxtop"><span class="txtitle">\n' +
                                    '取现流程需要进行短信确认，验证码已发送至管理员手机：'+shopdata.admin_phone+'，请按提示操作</span></p>' +
                                    '<p class="boxtop"><input type="test" class="yzms" placeholder="请输入手机验证码"   > <input type="button" onclick="getchecknumse('+shopdata.phone+')" value="获取验证码" class="getchecknumse"></p>' +
                                    '<p class="txtexts"></p>' +
                                    '<input type="hidden" class="txmoneys" value="'+txmoneys+'">' +
                                    '<input type="hidden" class="zzyhk" value="'+zzyhk+'">' +
                                    '<input type="hidden" class="khh" value="'+khh+'">' +
                                    '<input type="hidden" class="khr" value="'+khr+'">' +
                                    '</div>',
                                    btn: ['提交提现申请', '取消'],
                                    btn1: function (index,layero) {
                                        var khr = $('.khr').val();
                                        var khh = $(".khh ").val();
                                        var zzyhk = $(".zzyhk ").val();
                                        var txmoneys = $(".txmoneys ").val();
                                        var yzm = $(".yzms ").val();
                                        if (yzm == '') {
                                            layer.msg('请输入短信验证码');
                                            return;
                                        }
                                        //TODO 银行卡提现
                                        $.ajax({
                                            'type': 'POST',
                                            'url': '/withdraw/add-wihdraw',
                                            'data': {
                                                withdraw_money: txmoneys,
                                                type: 1,
                                                zzyhk: zzyhk,
                                                khh: khh,
                                                khr: khr,
                                                yzm: yzm
                                            },
                                            'dataType': 'json',
                                            'success': function (data) {
                                                if (data.code == 200) {
                                                    var shopmoney;
                                                    layer.msg('提现申请成功,待审核')
                                                    layer.close(index);
                                                    $('.yueval').html(data.data.money);
                                                    $('.txcount').html(data.data.count);
                                                } else {
                                                    layer.msg(data.msg)
                                                }
                                            }
                                        });
                                        }
                                })
                            }
                        });
                    }
                });
            } else if(isbtn == 'btnjl'){
                layer.open({
                    title:'提现列表',
                    type: 2,
                    area: ['960px', '630px'],
                    fix: false, //不固定
                    maxmin: true,
                    content: '/withdraw/index-list',
                    success:function(layero,index){
                    },
                    end:function(){

                    }
                });
            }
        } else {
            if(!shopdata.withdraw_pwd) {
                layer.open({
                    id: 10,
                    type: 1,
                    title: '设置提现密码',
                    skin: 'layui-layer-rim',
                    area: ['270px', 'auto'],
                    content: '<div style="margin-top: 20px;margin-left: 5px">' +
                    '<p class="boxtop"><input type="password"  class="txmm pwds"  placeholder="请输入新的提现密码"   >' +
                    '<p class="boxtop"><input type="password"  class="txmm reppwd"  placeholder="再次输入提现密码"   >' +
                    '<p class="boxtops">验证管理员手机:' + shopdata.admin_phone + '</p>' +
                    '<p class="boxtops"><input type="test" class="yzms" style="width: 100px" placeholder="请输入验证码"   > <input type="button" onclick="getchecknums('+shopdata.phone+')" value="获取验证码" class="getchecknums"></p>' +
                    '<p class="txtexts"></p>' +
                    '</div>',
                    btn: ['确认', '取消'],
                    btn1: function (index, layero) {
                        var pwds = $('.pwds').val();
                        var reppwd = $('.reppwd').val();
                        var yzm = $('.yzms').val();
                        $.ajax({
                            'type': 'POST',
                            'url': '/withdraw/update-withdraw-pwd',
                            'data': {pwd: pwds, reppwd: reppwd, yzm: yzm},
                            'dataType': 'json',
                            'success': function (data) {
                                if (data.code == 200) {

                                    layer.close(index)
                                    layer.msg(data.msg)
                                    window.location.reload()
                                } else {
                                    layer.msg(data.msg)
                                }
                            }
                        })
                    }
                })
                return;
            }


            layer.open({
                id: 1,
                type: 1,
                title: '账户密码验证',
                skin: 'layui-layer-rim',
                area: ['270px', 'auto'],
                content: '<div style="margin-top: 20px;margin-left: 5px">' +
                '<p class="boxtop"><input type="password"  class="txmm"  placeholder="请输入提现密码"   >' +
                '<p class="txtexts"></p>' +
                '</div>',
                btn: ['确认', '忘记密码','取消'],
                btn1: function (index, layero) {
                    var pwd = $('.txmm').val();
                    $.ajax({
                        'type':'POST',
                        'url':'/withdraw/checkpwd',
                        'data':{pwd:pwd},
                        'dataType':'json',
                        'success':function(data)
                        {
                            if(data.code == 200){
                                $('.yueval').html(shopdata.shop_money);
                                $('.txcount').html(shopdata.counts);
                                layer.close(index)
                                $('.hidpwd').val(data.data);

                            }else{
                                layer.msg(data.msg)
                            }
                        }
                    })
                },
                btn2: function (index, layero) {
                    layer.open({
                        id: 2,
                        type: 1,
                        title: '修改密码',
                        skin: 'layui-layer-rim',
                        area: ['270px', 'auto'],
                        content: '<div style="margin-top: 20px;margin-left: 5px">' +
                        '<p class="boxtop"><input type="password"  class="txmm pwds"  placeholder="请输入新的提现密码"   >' +
                        '<p class="boxtop"><input type="password"  class="txmm reppwd"  placeholder="再次输入提现密码"   >' +
                        '<p class="boxtops">验证管理员手机:'+shopdata.admin_phone+'</p>' +
                        '<p class="boxtops"><input type="test" class="yzms" s4tyle="width: 100px" placeholder="请输入验证码"    > <input type="button" onclick="getchecknum('+shopdata.phone+')" value="获取验证码" class="getchecknum"></p>' +
                        '<p class="txtexts"></p>' +
                        '</div>',
                        btn: ['确认', '取消'],
                        btn1: function (index, layero) {
                            var pwds = $('.pwds').val();
                            var reppwd = $('.reppwd').val();
                            var yzm = $('.yzms').val();
                            if(yzm == ''){
                                layer.msg('请输入短信验证码');
                                return;
                            }
                            if(pwds == ''){
                                layer.msg('请输入新密码');
                                return;
                            }
                            if(reppwd == ''){
                                layer.msg('请输入短信验证码');
                                return;
                            }
                            $.ajax({
                                'type':'POST',
                                'url':'/withdraw/update-withdraw-pwd',
                                'data':{pwd:pwds,reppwd:reppwd,yzm:yzm},
                                'dataType':'json',
                                'success':function(data)
                                {
                                    if(data.code == 200){
                                        layer.close(index)
                                        layer.msg(data.msg)
                                    }else{
                                        layer.msg(data.msg)
                                    }
                                }
                            })
                        }
                    })
                }
            })
            $('.txmm').focus();
        }

    })
})


function xzasdasd($this) {
    var money = $($this).val();
    var fl = money*0.01;
    $('.fwf').html(fl.toFixed(2));
}

function getshopmoney() {
    var shopmoneys;
    $.ajax({
        'type': 'POST',
        'url': '/withdraw/getshop',
        'async': false,
        'dataType': 'json',
        'success': function (data) {
            shopmoneys = data.data.money
        }
    })
    return shopmoneys;
}
function getchecknum(phone){
    getCheckNum(phone,".getchecknum");
}

function getchecknums(phone){
    getCheckNum(phone,".getchecknums");
}

function getchecknumse(phone){
    getCheckNum(phone,".getchecknumse");
}
// 获取验证码
function getCheckNum(phoneNum,CheckNumBtn){
    var phone_num=phoneNum;
    if(/^[1][3,4,5,7,8][0-9]{9}$/.test(phone_num)){
        var second=90;
        var timer=setInterval(function(){
            second--;
            if(second!=0){
                $(CheckNumBtn).css({"background":"#999","color":"#eee"}).val("重新发送"+second+"s");
                $(CheckNumBtn).attr("disabled","disabled");
            }else{
                $(CheckNumBtn).val("获取验证码").css({"background":"#08c","color":"#fff"});
                $(CheckNumBtn).removeAttr("disabled");
                clearInterval(timer);
            }
        },1000);
        ajax("post","/code/send-code",{phone:phone_num},function success(data){
            if(data.code==200){
                layer.msg(data.msg);
            }else{layer.msg(data.msg);}
        });
    }else{
        layer.msg("您输入的电话有误，请重新输入")
    }
}